//
//  TabViewController.m
//  MKUITestApp
//
//  Created by sushangjin on 2020/6/5.
//  Copyright © 2020 sushangjin. All rights reserved.
//

#import "TabViewController.h"
#import "TableViewController.h"
#import "CollectionViewController.h"
#import "PickerViewController.h"
#import "MenuTableViewController.h"
#import "Menu.h"
#import "MKUIElementListViewCellSelection+Test.h"
@interface TestTabParamViewController : UIViewController
@property(nonatomic,strong) MKUIListView *listView;
@property(nonatomic,strong) MKUIElementListViewCellSwitch *badgeSwitchElement;
@property(nonatomic,strong) MKUIElementListViewCellTextInput *badgeInputElement;
@property(nonatomic,weak) MKUITabBarController *tabController;
@property(nonatomic,weak) TabViewController *viewController;
@property(nonatomic,assign) BOOL showGlobalParam;
@end
@implementation TestTabParamViewController
- (id)init{
    if(self=[super init]){
        self.title = @"Param";
        self.tabBarItem = self.mk_customTabBarItem;
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.listView = [[MKUIListView alloc] initWithFrame:self.view.bounds style:(MKUIListViewStyleInsetGrouped)];
    self.listView.separatorStyle = MKUIListViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.listView];
    [self reloadData];
}
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGRect bounds = self.view.bounds;
    self.listView.frame = bounds;
}
- (void)reloadData{
    @MK_WEAKIFY(self);
    [self.listView deleteAllSections];
    if(self.showGlobalParam){
        [self.listView addSection];
        {
            [self.listView addItem:[MKUIElementListViewCellSelection testElementMKUItemFlowCollectionViewScrollDirection:@"滚动方向" selected:self.tabController.itemFlowContainer.flowColletionView.scrollDirection whenDone:^(MKUItemFlowCollectionViewScrollDirection selectedValue) {
                self.tabController.itemFlowContainer.flowColletionView.scrollDirection = selectedValue;
                [self.viewController reloadData];
            }]];
        }
        
        [self.listView addSection];
        {
            MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
            el.elementView.titleText = @"显示自定义选中指示器";
            el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
                @MK_NORMALIZE(self);
                self.tabController.itemFlowContainer.itemIndicatorViewClass = on?TabBarItemIndicatorView.class:MKUItemFlowIndicatorLineView.class;
                [self.tabController.itemFlowContainer reloadData];
            };
            el.elementView.on = self.tabController.itemFlowContainer.itemIndicatorViewClass==TabBarItemIndicatorView.class;
            [self.listView addItem:el];
        }
        
        [self.listView addSection];
        {
            MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
            el.elementView.titleText = @"显示自定义分隔线";
            el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
                @MK_NORMALIZE(self);
                self.tabController.itemFlowContainer.separatorViewClass = on?TabBarItemSeparatorView.class:MKUItemFlowSeparatorView.class;
                [self.tabController.itemFlowContainer reloadData];
            };
            el.elementView.on = self.tabController.itemFlowContainer.separatorViewClass==TabBarItemSeparatorView.class;
            [self.listView addItem:el];
        }
        {
            [self.listView addItem:[MKUIElementListViewCellSelection testUIColorElementWithTitle:@"分隔线颜色" selected:self.tabController.itemFlowContainer.separatorColor whenDone:^(UIColor * _Nonnull color) {
                self.tabController.itemFlowContainer.separatorColor = color;
                [self.tabController.itemFlowContainer reloadData];
            }]];
        }
    }
    
//    TabBarItemIndicatorView;
    [self.listView addSection];
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"显示自定义TabBarCell";
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.mk_customTabBarItem.itemCellClass = on?TabBarItemCollectionViewCell.class:nil;
            [self.tabController.itemFlowContainer reloadData];
        };
        el.elementView.on = self.mk_customTabBarItem.itemCellClass==TabBarItemCollectionViewCell.class;
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"Badge";
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.tabBarItem.badgeValue = on?self.badgeInputElement.elementView.text:nil;
        };
        el.elementView.on = self.tabBarItem.badgeValue!=nil;
        self.badgeSwitchElement = el;
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellTextInput *el = [[MKUIElementListViewCellTextInput alloc] init];
        el.elementView.titleText = @"角标Badge";
        el.elementView.placeholder = @"请输入角标值";
        el.elementView.text = self.tabBarItem.badgeValue;
        el.elementView.whenDone = ^(__kindof MKUIElementViewTextInput * _Nonnull el) {
            @MK_NORMALIZE(self);
            self.tabBarItem.badgeValue = self.badgeSwitchElement.elementView.isOn?self.badgeInputElement.elementView.text:nil;
        };
        self.badgeInputElement = el;
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"是否显示圆点角标";
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.mk_customTabBarItem.badgeStyle = on?MKUICustomTabBarItemBadgeStyleDot:MKUICustomTabBarItemBadgeStyleText;
            
        };
        el.elementView.on = self.mk_customTabBarItem.badgeStyle==MKUICustomTabBarItemBadgeStyleDot;
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellTextInput *el = [[MKUIElementListViewCellTextInput alloc] init];
        el.elementView.titleText = @"标题";
        el.elementView.placeholder = @"请输入标题";
        el.elementView.text = self.tabBarItem.title;
        el.elementView.whenDone = ^(__kindof MKUIElementViewTextInput * _Nonnull el) {
            @MK_NORMALIZE(self);
            self.tabBarItem.title = el.text;
            [self.tabController.itemFlowContainer reloadData];
        };
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"图片";
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.tabBarItem.image = on?[Menu randomImage]:nil;
            [self.tabController.itemFlowContainer reloadData];
        };
        el.elementView.on = self.tabBarItem.image!=nil;
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"选中图片";
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.tabBarItem.selectedImage = on?[Menu randomImage]:nil;
            [self.tabController.itemFlowContainer reloadData];
        };
        el.elementView.on = self.tabBarItem.selectedImage!=nil;
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.titleText = @"删除";
        el.elementView.buttonStyle = MKUIElementViewButtonStyleDestructive;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            NSMutableArray *contents = [self.tabController.contents mutableCopy];
            [contents removeObject:self];
            self.tabController.contents = contents;
            [self.tabController reloadDataWithAnimated:YES];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.titleText = @"添加";
        el.elementView.buttonStyle = MKUIElementViewButtonStyleDefault;
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            NSMutableArray *contents = [self.tabController.contents mutableCopy];
            TestTabParamViewController *vc = [TestTabParamViewController new];
            vc.tabController = self.tabController;
            vc.title = [NSString stringWithFormat:@"Param_new_%@",@(contents.count+1)];
            NSInteger index = [contents indexOfObject:self];
            [contents insertObject:vc atIndex:index];
            self.tabController.contents = contents;
            [self.tabController reloadDataWithAnimated:YES];
        };
        [self.listView addItem:el];
    }
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.listView deselectAllItemsWithAnimated:YES];
}

@end

@interface TabViewController ()<MKUITabBarControllerDelegate>
@property(nonatomic,strong) MKUITabBarController *tabController;
@end

@implementation TabViewController
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"Tab" viewControllerClass:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([UIDevice currentDevice].systemVersion.floatValue<11.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    //初始化tab控制器，指定标签容器、内容容器实现类
    self.tabController = [[MKUITabBarController alloc] initWithItemContainerClass:[MKUITabBarItemFlowContainer class] contentContainerClass:[MKUITabBarContentContainerListView class]];
    self.tabController.delegate = self;
    //添加标签视图、内容视图
    [self.view addSubview:self.tabController.itemContainer];
    [self.view addSubview:self.tabController.contentContainer];
    self.tabController.itemContainer.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.tabController.itemContainer.layer.borderWidth = 1;
    
    //
    NSMutableArray<UIViewController *> *viewcontrollers = [[NSMutableArray alloc] init];
    for(int i=0;i<5;i++){
        TestTabParamViewController *vc = [TestTabParamViewController new];
        vc.showGlobalParam = i==0;
        vc.tabController = self.tabController;
        vc.viewController = self;
        vc.title = [NSString stringWithFormat:@"Param_%@",@(i)];
        [viewcontrollers addObject:vc];
    }
    self.tabController.contents = viewcontrollers;
    //设置默认选中内容
    self.tabController.selectedIndex = 0;
    [self reloadData];
    [self __reloadNavItem];
}
- (void)dealloc{
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tabController.selectedContent tabBarContentViewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tabController.selectedContent tabBarContentViewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.tabController.selectedContent tabBarContentViewWillDisapper:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.tabController.selectedContent tabBarContentViewDidDisapper:animated];
}
- (void)reloadData{
    [self.view setNeedsLayout];
    BOOL horizontal = self.tabController.itemFlowContainer.flowColletionView.scrollDirection==MKUItemFlowCollectionViewScrollDirectionHorizontal;
    self.tabController.listViewContentContainer.listView.scrollDirection = horizontal?MKUIListViewScrollDirectionHorizontal:MKUIListViewScrollDirectionVertical;
    self.tabController.itemFlowContainer.separatorSize = horizontal?CGSizeMake(1, 20):CGSizeMake(80, 1);
    [self.tabController reloadData];
}
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGRect bounds = self.safeBounds;
    CGRect oldFrame = self.tabController.contentContainer.frame;
    
    if(self.tabController.itemFlowContainer.flowColletionView.scrollDirection==MKUItemFlowCollectionViewScrollDirectionHorizontal){
        CGRect f1 = bounds;
        f1.size.height = [self.tabController.itemContainer sizeThatFits:bounds.size].height;
        f1.origin.y = CGRectGetMinY(bounds);
        self.tabController.itemContainer.frame = f1;
        //
        CGRect f2 = bounds;
        f2.origin.y = CGRectGetMaxY(f1);
        f2.size.height = CGRectGetMaxY(bounds)-f2.origin.y;
        self.tabController.contentContainer.frame = f2;
    }else{
        CGRect f1 = bounds;
        f1.size.width = 80;
        f1.origin.x = CGRectGetMinX(bounds);
        self.tabController.itemContainer.frame = f1;
        //
        CGRect f2 = bounds;
        f2.origin.x = CGRectGetMaxX(f1);
        f2.size.width = CGRectGetMaxX(bounds)-f2.origin.x;
        self.tabController.contentContainer.frame = f2;
    }
    
    BOOL sizechange = !CGSizeEqualToSize(oldFrame.size, self.tabController.contentContainer.frame.size);
    if(sizechange){
        [self.tabController reloadData];
    }
}
#pragma mark - delegate:MKUITabBarControllerDelegate
- (BOOL)tabBarController:(MKUITabBarController *)controller shouldSelectIndex:(NSInteger)index{
    if(index==controller.contents.count-1){
        MKUIAlertController *alert = [MKUIAlertController alertControllerWithTitle:@"选择" message:[NSString stringWithFormat:@"选中%@",@(index)] preferredStyle:(MKUIAlertViewStyleAlert)];
        [alert addAction:[MKUIAlertAction actionWithTitle:@"确定" style:(MKUIAlertActionStyleDefault) handler:nil]];
        [alert show];
        return NO;
    }
    return YES;
}
- (void)tabBarController:(MKUITabBarController *)controller didSelectIndex:(NSInteger)index{
    [self __reloadNavItem];
}
- (void)__reloadNavItem{
    self.navigationItem.title = self.tabController.selectedContent.tabBarItem.title;
    UIViewController *v = nil;
    if([self.tabController.selectedContent isKindOfClass:[UIViewController class]]){
        v = (UIViewController *)self.tabController.selectedContent;
    }
    self.navigationItem.rightBarButtonItems = v.navigationItem.rightBarButtonItems;
    self.navigationItem.leftBarButtonItems = v.navigationItem.leftBarButtonItems;
}
@end


@implementation TabBarItemContainerCollectionView
@end
@implementation TabBarItemIndicatorView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.2];
    }
    return self;
}
@end
@implementation TabBarItemSeparatorView
- (void)customReloadCellModel{
    [super customReloadCellModel];
}
@end
@implementation TabBarItemCollectionViewCell
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.tabBarItemButton.contentInsets = UIEdgeInsetsMake(10, 15, 10, 15);
        self.tabBarItemButton.titleLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.tabBarItemButton.titleLabel.layer.borderWidth = 1;
    }
    return self;
}
- (UIView *)itemIndicatorRectView{
    return self.tabBarItemButton.titleLabel;
}
+ (UIColor *)titleColorWithSelected:(BOOL)selected{
    return selected?[UIColor systemRedColor]:[UIColor mk_colorWithLight:[UIColor blackColor] dark:[UIColor whiteColor]];
}
MKDEF_SINGLETON(TabBarItemCollectionViewCell)
@end
